import React from 'react';
import { Row, Col, Form, Radio, DatePicker, Input, InputNumber, Select } from 'antd';
import moment from 'moment';
import { Link } from 'umi';

import { getDict } from '@/utils/dataDictUtil';
import { useRequest } from '@/hooks';
import { LABOUR_PERIOD_TYPE, PROBATION_PERIOD_TYPE } from '@/pages/Recruitment/dictionary';
import { useNoLabourPeriod } from '@/pages/Recruitment/Hire/Communicate/hooks';

import { formItemLayout, oneLineLayout, renderItem, adjustRenderItem, getDictName } from '../util';

const { Option } = Select;
const { TextArea } = Input;

export default function HireInfo({ isEdit = true, form, applyData, hireInfo }) {
    const { data: JOB_TYPE } = useRequest(() => getDict('jobNature'), { initialData: [] });
    const disabledDate = current => current && current < moment().startOf('day');

    const { isNoLabourPeriod, noLabourPeriodCheckbox, noLabourPeriodText } = useNoLabourPeriod({
        applyData,
        form,
        formItemId: 'labourPeriodNumber'
    });
    return (
        <>
            <Row>
                <Col span={12}>
                    <Form.Item label="入职岗位" {...formItemLayout}>
                        {applyData.entryDutyName}
                    </Form.Item>
                </Col>
                <Col span={12}>
                    <Form.Item label="入职部门" {...formItemLayout}>
                        {applyData.organizationName}
                    </Form.Item>
                </Col>
            </Row>
            <Row>
                <Col span={12}>
                    <Form.Item label="工作性质" {...formItemLayout}>
                        {adjustRenderItem(
                            form?.getFieldDecorator('workType', {
                                initialValue: applyData.workType || JOB_TYPE?.[0]?.value
                            })(
                                <Radio.Group>
                                    {JOB_TYPE.map(item => (
                                        <Radio value={item.value} key={item.value}>
                                            {item.text}
                                        </Radio>
                                    ))}
                                </Radio.Group>
                            ),
                            getDictName(applyData?.workType, JOB_TYPE),
                            isEdit
                        )}
                    </Form.Item>
                </Col>
            </Row>
            <Row>
                <Col span={12}>
                    <Form.Item label="建议入职时间" {...formItemLayout}>
                        {adjustRenderItem(
                            form?.getFieldDecorator('suggestedEntryTime', {
                                initialValue: moment(applyData?.suggestedEntryTime),
                                rules: [
                                    {
                                        required: true,
                                        message: '建议入职时间不能为空'
                                    }
                                ]
                            })(
                                <DatePicker
                                    disabledDate={disabledDate}
                                    showTime={{ format: 'HH:mm' }}
                                    format="YYYY-MM-DD"
                                    placeholder="请选择建议入职时间"
                                    style={{ width: '100%' }}
                                />
                            ),
                            applyData?.suggestedEntryTime && moment(applyData?.suggestedEntryTime).format('YYYY-MM-DD'),
                            isEdit
                        )}
                    </Form.Item>
                </Col>
                <Col span={12}>
                    <Form.Item label="劳动合同年限" {...formItemLayout}>
                        {adjustRenderItem(
                            <>
                                {form?.getFieldDecorator('labourPeriodNumber', {
                                    initialValue: applyData.labourPeriodNumber || 3,
                                    rules: [
                                        {
                                            required: true,
                                            message: '劳动合同年限不能为空'
                                        }
                                    ]
                                })(<InputNumber disabled={isNoLabourPeriod} min={0} />)}
                                <span style={{ padding: '0 6px' }}></span>
                                {form?.getFieldDecorator('labourPeriodType', {
                                    initialValue: applyData.labourPeriodType || LABOUR_PERIOD_TYPE?.[0].value
                                })(
                                    <Select
                                        getPopupContainer={triggerNode => triggerNode.parentElement}
                                        style={{ width: 80 }}
                                        disabled={isNoLabourPeriod}
                                    >
                                        {LABOUR_PERIOD_TYPE.map(item => (
                                            <Option value={item.value} key={item.value}>
                                                {item.text}
                                            </Option>
                                        ))}
                                    </Select>
                                )}
                                <span style={{ padding: '0 6px' }}></span>
                                {noLabourPeriodCheckbox}
                            </>,
                            isNoLabourPeriod
                                ? noLabourPeriodText
                                : `${applyData?.labourPeriodNumber ?? ''}${getDictName(
                                      applyData?.labourPeriodType,
                                      LABOUR_PERIOD_TYPE
                                  )}`,
                            isEdit
                        )}
                    </Form.Item>
                </Col>
                <Col span={12}>
                    <Form.Item label="试用期" {...formItemLayout}>
                        {adjustRenderItem(
                            <>
                                {form?.getFieldDecorator('probationPeriodNumber', {
                                    initialValue: applyData.probationPeriodNumber || 6,
                                    rules: [
                                        {
                                            required: true,
                                            message: '试用期不能为空'
                                        }
                                    ]
                                })(<InputNumber min={0} />)}
                                <span style={{ padding: '0 6px' }}></span>
                                {form?.getFieldDecorator('probationPeriodType', {
                                    initialValue: applyData.probationPeriodType || PROBATION_PERIOD_TYPE?.[0].value
                                })(
                                    <Select
                                        getPopupContainer={triggerNode => triggerNode.parentElement}
                                        style={{ width: 80 }}
                                    >
                                        {PROBATION_PERIOD_TYPE.map(item => (
                                            <Option value={item.value} key={item.value}>
                                                {item.text}
                                            </Option>
                                        ))}
                                    </Select>
                                )}
                            </>,
                            `${applyData?.probationPeriodNumber ?? ''}${getDictName(
                                applyData?.probationPeriodType,
                                PROBATION_PERIOD_TYPE
                            )}`,
                            isEdit
                        )}
                    </Form.Item>
                </Col>
                <Col span={24}>
                    <Form.Item label="补充说明" {...oneLineLayout} {...(isEdit ? { help: '不超过200个字符' } : {})}>
                        {adjustRenderItem(
                            form?.getFieldDecorator('remark', {
                                initialValue: applyData.remark
                            })(<TextArea maxLength={200} placeholder="请输入审批补充信息" style={{ height: 100 }} />),
                            applyData?.remark,
                            isEdit
                        )}
                    </Form.Item>
                </Col>
            </Row>
            <Row>
                {renderItem({
                    label: '通知时间',
                    value: (
                        <>
                            录用审批通过后系统自动发送录用通知邮件给录用候选人{' '}
                            {hireInfo && (
                                <Link
                                    to={`/user/hireConfirm?employId=${applyData?.employId ||
                                        applyData?.employId}&action=detail`}
                                >
                                    预览录用信息
                                </Link>
                            )}
                        </>
                    ),
                    span: 24,
                    layout: oneLineLayout
                })}
            </Row>
        </>
    );
}
